package com.mengrv.springbootdemo.controller;

import com.mengrv.springbootdemo.model.Response;
import com.mengrv.springbootdemo.model.User;
import com.mengrv.springbootdemo.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(value = "/users")     // 通过这里配置使下面的映射都在/users下，可去除
public class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation(value = "用户登录", notes = "用户使用用户账号、密码登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账号", defaultValue = "mengrui", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", defaultValue = "123456", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    Response login(@RequestParam String account, @RequestParam String password) throws Exception {
        User user = userService.getUserByAccount(account);
        Response response = new Response();
        if (user == null) {
            response.setError("手机号未注册！");
        } else if (user.getPassword().equals(password)) {
            user.newToken();
            userService.updateToken(user.getToken(), user.getUserID());
            user.setPassword(null);
            response.setContent(user);
        } else {
            response.setError("密码错误");
        }
        return response;
    }

    @ApiOperation(value = "修改密码", notes = "使用账号、旧密码更改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账号", defaultValue = "mengrui", required = true, dataType = "String"),
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", defaultValue = "123456", required = true, dataType = "String"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", defaultValue = "123456", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/{account}/password", method = RequestMethod.PUT)
    public Response putPassword(@PathVariable String account, @RequestParam String oldPassword, @RequestParam String newPassword) throws Exception {
        Response response = new Response();
        User user = userService.getUserByAccount(account);
        if (user == null) {
            response.setError("账号不存在！");
        } else if (user.getPassword().equals(oldPassword)) {
            user.newToken();
            userService.updatePasswordByAccount(account, newPassword);
            userService.updateToken(user.getToken(), user.getUserID());
        } else {
            response.setError("原密码不对！");
        }
        return response;
    }

    @ApiOperation(value = "修改用户信息", notes = "更改用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", defaultValue = "mengrui", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mobile", value = "手机号", defaultValue = "15900533302", required = true, dataType = "String"),
            @ApiImplicitParam(name = "birthday", value = "生日", defaultValue = "721238400000", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "userName", value = "用户名", defaultValue = "管理员", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/{account}/userInfo", method = RequestMethod.PUT)
    public Response putUserInfo(@PathVariable String account, @RequestParam String mobile, @RequestParam Long birthday, @RequestParam String userName) {
        Response response = new Response();
        User user = new User();
        user.setAccount(account);
        user.setUserName(userName);
        user.setBirthday(birthday);
        user.setMobile(mobile);
        if (userService.updateUserInfo(user) != 1) {
            response.setError("账号不存在");
        }
        return response;
    }


    @ApiOperation(value = "注册用户", notes = "注册用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "用户信息", required = true, dataType = "User")
    })
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Response postUser(@RequestBody User user) {
        Response response = new Response();
        User _user = userService.getUserByAccount(user.getAccount());
        if (_user != null) {
            response.setError("账号已存在！");
        } else {
            userService.postUser(user);
        }
        return response;
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户账号", defaultValue = "string", required = true, dataType = "String")
    })
    @RequestMapping(value = "/{account}", method = RequestMethod.DELETE)
    public Response deleteUser(@PathVariable String account, HttpServletRequest request) {
        Response response = new Response();
        User user = userService.getUserByAccount(account);
        String token = request.getHeader("token");
        if (token != null && token.equals(user.getToken())) {
            userService.deleteByAccount(account);
        } else {
            response.setError("只有用户自己可以删除自己的账号！");
        }
        return response;
    }

    @ApiOperation(value = "获取用户列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "offset", value = "起始数据索引", defaultValue = "0", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "查询数据长度", defaultValue = "10", required = true, dataType = "int")
    })
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Response getUserList(@RequestParam int offset, @RequestParam int limit) {
        Response response = new Response();
        response.setContent(userService.getUserList(offset, limit));
        return response;
    }

}
